<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>login_check</title>
</head>
<body>
<%
    // 判断是否有请求内容
    // 在自提交的页面中，必须对程序第一次运行做出处理
    int state=-1;
    request.setCharacterEncoding("UTF-8");
    if(request.getParameter("name")!=null&&request.getParameter("passwd")!=null){
        // 第一次的时候，并不能取得请求的参数
        state = 0;
        String name = request.getParameter("name");
        String passwd = request.getParameter("passwd");
        // out.println(name+", "+passwd);
        try {
            Class.forName("com.mysql.cj.jdbc.Driver").newInstance(); //隐式装载驱动程序
        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        String url="jdbc:mysql://127.0.0.1:3306/test? characterEncoding=utf8 & useSSL=false & serverTimezone=UTC & rewriteBatchedStatements=true";
        String user="exp"; //数据库的用户名
        String password="javaweb"; //数据库的密码
        Connection conn= null; //建立连接
        try {
            conn = DriverManager.getConnection(url,user,password);
            String strSql = "select studentPwd from students where studentName=?";
            PreparedStatement pStatement = conn.prepareStatement(strSql);
            pStatement.setString(1, name);
            ResultSet resSet=pStatement.executeQuery(); //3.语句总管执行SQL语句
            state = 1;
            if (resSet.next()) {
                String ret = resSet.getString(1);
                if (ret.equals(passwd)) {
                    state = 2; // 该用户存在
                    session.setAttribute("flag", "ok");
                    session.setAttribute("name", name);

                    if(application.getAttribute("count") == null){
                        application.setAttribute("count", 0);
                    }
                    int count = (Integer)application.getAttribute("count");
                    count++;
                    application.setAttribute("count",count);

                    // 跳转到welcome.jsp
                    response.sendRedirect("welcome.jsp");
                } else {
                    state = 3;  // 密码错误
                }
            }
            resSet.close();
            pStatement.close();
            conn.close(); //关闭连接
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    String alertStr = null;

    switch (state){
        case -1:alertStr="无效字符输入！";break;
        case 0:alertStr="数据库错误！";break;
        case 1:alertStr="该用户不存在！";break;
        case 3:alertStr="密码错误！";break;
    }
    out.print(String.format("<script type='text/javascript'>alert(\"%s\");</script>",alertStr));
    response.setHeader("refresh", "0.0001;URL=login.jsp");
%>
</body>
</html>